\name{GetSpeciesCode}
\alias{GetSpeciesCode}
%- Also NEED an '\alias' for EACH other topic documented here.
\title{ Get AOU species code from Common or scientific name }
\description{
  This function retrieves the AOU species code from a common name or scientific name
}
\usage{
GetSpeciesCode(species, sci.name = FALSE, ODBC = FALSE, batch = FALSE, ...)
}
%- maybe also 'usage' for other objects documented here.
\arguments{
  \item{species}{ A vector of common or scientific names }
  \item{sci.name}{ Boolean to denote whether the species vector is of common or scientific names }
  \item{ODBC}{ Boolean to determine whether an ODBC connection should be used instead of DBI (via RMySQL) }
  \item{batch}{Boolean variable to denote whether this process should be done in batch mode}
  \item{\dots}{Additional variables that can get passed through to underlying functions}}
}
\details{
  When the \code{batch} argument is set to TRUE, this allows
  the matching process to occur on the users computer after
  one complete download of the species table.  This will be
  much faster if a substantial number of queries need to be
  made.  However, if the number of queries is small then 
  leaving \code{batch} as FALSE will be faster, since the
  database will not need to be downloaded.

}
\value{
  A vector the same length as the input vector that contains
  corresponding AOU species codes.
}
\references{ \url{http://www.pwrc.usgs.gov/bbl/manual/aspeclst.htm} }
\author{ Mark Herzog \email{mherzog@prbo.org}}
\seealso{ \code{\link{GetSpeciesName}}}
\examples{
##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--    or do  help(data=index)  for the standard data sets.

## The function is currently defined as
function (species, sci.name=FALSE, ODBC=FALSE, batch=FALSE, ...) {
    if (is.null(species)==TRUE) stop("Error: No species name given")
    specquery.main<-paste("FROM FieldBird, FieldBirdTaxon, TaxonCommonName, Taxon",
                            "WHERE TaxonCommonName.TaxonId = FieldBirdTaxon.TaxonId",
                            "AND Taxon.TaxonId=FieldBirdTaxon.TaxonId",
                            "AND FieldBird.FieldBirdId = FieldBirdTaxon.FieldBirdId",
                            "AND FieldBird.BirdCdValidEndDt is NULL",
                            "AND language='English'")
    specquery.end<-""
    if (sci.name==FALSE) {
        specquery.start<-"SELECT TaxonCommonName.CommonName as species, FieldBird.BirdCd as spec"
        if (batch==FALSE) specquery.end<-paste("AND TaxonCommonName.CommonName IN (",paste("'",species,"'",sep="",collapse=","),")",sep="")
    } else {
        specquery.start<-"SELECT Taxon.TaxonName as species, FieldBird.BirdCd as spec"
        if (batch==FALSE) specquery.end<-paste("AND Taxon.TaxonName IN(",paste("'",species,"'",sep="", collapse=","),")",sep="")
    }    
    specquery<-paste(specquery.start,specquery.main,specquery.end,"GROUP BY species")
    query.results<-fetch(dbSendQuery(db<-PRBOdbConnect(...),specquery),n=-1)
#    merge.results<-merge(data.frame(species),query.results, all.x=TRUE,sort=FALSE,by="species")
#    SpeciesCode<-merge.results$spec
    SpeciesCode<-query.results$spec[match(species,query.results$species)]
    if (ODBC==TRUE) {
        odbcClose(db)
    } else {
        dbDisconnect(db)
    }
    return(SpeciesCode)
  }
}
% Add one or more standard keywords, see file 'KEYWORDS' in the
% R documentation directory.
\keyword{ data }
